﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ChinaCustoms.Qingdao.Workflow.Permissions;
using ChinaCustoms.Framework.DeluxeWorks.Library.OGUPermission;

namespace ChinaCustoms.Qingdao.Workflow.Activities.Chooser
{
    public partial class OrganizationForm : Form
    {
        private int displayLevels = -1;
        /// <summary>
        /// 要显示到的部门级别
        /// </summary>
        public int DisplayLevels
        {
            get { return displayLevels; }
            set { displayLevels = value; }
        }

        private IList<TNTreeNode> selectedNodes;
        /// <summary>
        /// 被选中的节点
        /// </summary>
        public IList<TNTreeNode> SelectedNodes
        {
            get { return selectedNodes; }
            set { selectedNodes = value; }
        }
	

        /// <summary>
        /// 构造函数
        /// </summary>
        public OrganizationForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 被选中的部门ID
        /// </summary>
        public string OrganizationIDs 
        {
            get 
            {
                string ret = string.Empty;

                if (selectedNodes == null)
                {
                    selectedNodes = new List<TNTreeNode>();
                    GetSelectedNodes((TNTreeNode)this.treeView1.Nodes[0]);
                }

                for (int i = 0; i < selectedNodes.Count; i++)
                {
                    ret += selectedNodes[i].ID + ",";
                }

                if (ret.EndsWith(","))
                {
                    ret = ret.Remove(ret.Length - 1);
                }

                this.textBox1.Text = ret;
                return ret;
            }
            set { this.textBox1.Text= value;}
        }

        /// <summary>
        /// 被选中的部门名称
        /// </summary>
        public string OrganizationNames
        {
            get
            {
                string ret = string.Empty;

                if (selectedNodes == null)
                {
                    selectedNodes = new List<TNTreeNode>();
                    GetSelectedNodes((TNTreeNode)this.treeView1.Nodes[0]);
                }

                for (int i = 0; i < selectedNodes.Count; i++)
                {
                    ret += selectedNodes[i].Text + ",";
                }

                if (ret.EndsWith(","))
                {
                    ret = ret.Remove(ret.Length - 1);
                }

                return ret;
            }
        }


        private void GetSelectedNodes(TNTreeNode parentNode)
        {
            if (parentNode.Checked == true)
            {
                selectedNodes.Add(parentNode);
            }

            if (parentNode.Nodes.Count > 0)
            {
                foreach (TNTreeNode tempnode in parentNode.Nodes)
                {
                    GetSelectedNodes(tempnode);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void OrganizationForm_Load(object sender, EventArgs e)
        {
            this.BuildeTree();
        }

        private void BuildeTree()
        {
            this.treeView1.ShowLines = true;
            this.treeView1.ShowNodeToolTips = true;
            this.treeView1.ShowPlusMinus = true;
            this.treeView1.ShowRootLines = true;
            this.treeView1.Nodes.Clear();
            this.treeView1.CheckBoxes = true;

            IOrganization rootOrg = ChinaCustomsPermissionWrapper.GetRootOrganization();//permissionClient.GetRootOrganization();
            

            Dictionary<string, IOrganization> organizationCollectionAll = null;
            Dictionary<string, string> needDisplayOrg = null;
            ChinaCustomsPermissionWrapper.GetObjectsSearched(rootOrg,out needDisplayOrg, out organizationCollectionAll);

            if (rootOrg != null)
            {
                TNTreeNode rootNode = new TNTreeNode();
                rootNode.ID = rootOrg.ID;
                rootNode.Text = rootOrg.Name;
                this.treeView1.Nodes.Add(rootNode);
                this.BuildeTreeNode(rootNode, needDisplayOrg, organizationCollectionAll);
            }
        }

        private void BuildeTreeNode(TNTreeNode parentNode, Dictionary<string, string> needDisplayOrg, Dictionary<string, IOrganization> organizationCollectionAll)
        {
            if (needDisplayOrg == null || organizationCollectionAll == null)
            {
                return;
            }
            else
            {
                string parentID = parentNode.ID;

                foreach (KeyValuePair<string, string> kvpOrg in needDisplayOrg)
                {
                    if (kvpOrg.Value == parentID)
                    {
                        TNTreeNode node = new TNTreeNode();
                        node.ID = kvpOrg.Key;
                        node.Text = organizationCollectionAll[kvpOrg.Key].DisplayName;


                        this.BuildeTreeNode(node, needDisplayOrg, organizationCollectionAll);
                        parentNode.Nodes.Add(node);
                    }
                }
            }
        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {

        }
    }
}
